using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Entity.Infrastructure;
using ShopPowerTemplate.Services.Properties;
using ShopPowerTemplate.Entities;

namespace ShopPowerTemplate.Services
{
    public class ProductDetailService : ProductDetailServiceBase, IProductDetailService
    {
		public ProductDetailService(IEntitiesDB entitiesDB) : base(entitiesDB) { }

		//Custom code here
        public ProductDetail GetProductDetail(Product product, ICollection<Option> selectectOptions)
        {
            var options = selectectOptions.Select(so => so.Id);
            return EntitiesDB.ProductDetailSet
                .Include("Product")
                .Include("Option")
                .FirstOrDefault(pd =>
                    pd.Product.Id == product.Id &&
                    pd.Option.Select(o => o.Id).All(oid => options.Any(so => so == oid)));
        }

        //Override methods
        public override List<ProductDetail> FindAll()
        {
            return EntitiesDB.ProductDetailSet.Include("Product").Include("Option").OrderBy(x => x.Id).ToList();
        }

        public override ProductDetail Find(int id)
        {
            ProductDetail productDetail =
                EntitiesDB.ProductDetailSet
                    .Include("Product")
                    .Include("OrderDetails")
                    .Include("Option.OptionType")
                    .FirstOrDefault(x => x.Id == id);

            if (productDetail != null)
            {
                var productAux = productDetail.Product; //EF4.1 bug hack
            }
            return productDetail;
        }
    }
}	

